<template>
	<view class="cl_flex_page">
		<cl-nav-bar :title="title"></cl-nav-bar>
		<view class="title_form">
			<text class="fs_28">模板名称</text>
			<uni-easyinput class="ml_20" v-model="formData.name" placeholder="请输入模板名称"></uni-easyinput>
		</view>
		<view class="tempalte_fields">
			<view class="p_20 fs_24">字段设置</view>
			<l-drag :list="formData.fieldList" @change="getNewList" ref="dragRef" :column="1" :touchHandle="touchHandle"
				gridHeight="90rpx" handle>
				<template #grid="{ active, content, oindex, index }">
					<view class="tempalte_fields_item" :class="{ tempalte_fields_item_active: active }"
						@click.stop="handleEditField(content)">
						<view class="flex_end_center">
							<image class="w_50 h_50 mr_20" src="../../static/del_icon.png"
								@click.stop="handleDel($event, index, oindex)" />
							<text> {{ content.name }} </text>
						</view>

						<view class="flex_end_center">
							<text class="tempalte_fields_item_type">{{
			          content.typeName
			        }}</text>
							<uni-icons style="margin-left: 4rpx" type="right" size="18"></uni-icons>
							<uni-icons style="margin-left: 4rpx" type="bars" size="18" @touchstart="touchHandle = true"
								@touchend="touchHandle = false"></uni-icons>
						</view>
					</view>
				</template>
			</l-drag>
			<view class="tempalte_add" @click="handleAddField">
				<image class="tempalte_add_img" src="../../static/add_icon.png" />
				<text class="tempalte_add_text">新增字段</text>
			</view>
		</view>
		<view class="cl_bottom_box">
			<view class="cl_bottom_box_btn" @click="handleSaveTemplate">
				保存模板
			</view>
		</view>
	</view>
	<FieldSetting ref="fieldSettingRef" @saveField="handleSaveField"></FieldSetting>
</template>

<script setup>
	import {
		onLoad
	} from '@dcloudio/uni-app'
	import {
		computed,
		ref
	} from 'vue'
	import {
		getUuid
	} from '@/utils/uuid.js'
	import {
		formatDate,
		cloneDeep,
		isEmpty
	} from '@/utils/index.js'
	import FileApi from '@/utils/file.js'
	import FieldSetting from './components/index.vue'
	const fieldSettingRef = ref()
	const dragRef = ref()
	const type = ref()
	const title = computed(() => {
		if (type.value === 'add') {
			return '创建模板'
		} else if (type.value === 'edit') {
			return '修改模板'
		} else if (type.value === 'detail') {
			return '模板详情'
		}
	})
	onLoad((option) => {
		type.value = option.type
	})

	const formData = ref({
		name: '',
		id: getUuid(),
		fieldList: []
	})

	const rightOptions = [{
		text: '删除',
		type: 'del',
		style: {
			"backgroundColor": '#dd524d'
		}
	}]

	const getNewList = (newList) => {
		let resList = newList.map((item) => item.content)
		formData.value.fieldList = resList
	}
	const touchHandle = ref(false)

	const handleAddField = () => {
		fieldSettingRef.value.setFeild('add')
	}

	// 编辑字段
	const handleEditField = (item) => {
		fieldSettingRef.value.setFeild('edit', cloneDeep(item))
	}

	// 保存字段
	const handleSaveField = (fieldData) => {
		if (fieldData.id) {
			let idx = formData.value.fieldList.findIndex(
				(item) => item.id === fieldData.id
			)
			if (idx > -1) {
				formData.value.fieldList.splice(editIdx, 1, fieldData)
			}
		} else {
			fieldData.field_id = getUuid()
			dragRef.value.push(fieldData)
		}
		uni.hideLoading()
	}

	// 保存模板
	const handleSaveTemplate = async () => {
		// 判断模板名称是否为空
		if (isEmpty(formData.value.name)) {
			return uni.showToast({
				title: '请输入模板名称！',
				mask: true,
				duration: 800
			})
		}
		if (type.value === 'add') {
			formData.value.createTime = formatDate('stamp')
		} else if (type.value === 'edit') {
			formData.value.editTime = formatDate('stamp')
		}
		if (formData.value.fieldList.length === 0) {
			return uni.showToast({
				title: '模板数据不能为空！',
				duration: 800,
				mask: true,
				icon: 'none'
			})
		}
		uni.showLoading({
			title: '模板保存中'
		})
		try {
			await FileApi.createTemplateJson(formData.value)
			uni.showToast({
				title: '创建成功！',
				icon: 'success',
				mask: true,
				duration: 800
			})
		} catch (err) {
			uni.showToast({
				title: err.message,
				icon: 'none',
				mask: true,
				duration: 800
			})
		}
		uni.navigateBack()
	}
</script>

<style scoped lang="scss">
	.title_form {
		padding: 20rpx;
		display: flex;
		align-items: center;
		background-color: $uni-bg-color;
	}

	.tempalte {
		&_fields {
			flex: 1;
			overflow: auto;

			&_text {
				padding-left: 20rpx;
				color: $uni-text-color-placeholder;
				font-size: 20rpx;
			}

			&_item {
				display: flex;
				justify-content: space-between;
				align-items: center;
				padding: 20rpx;
				border-bottom: 1px solid gainsboro;
				background-color: #fff;

				&_active {
					background-color: ghostwhite;
				}

				&_type {
					margin-left: 40rpx;
					font-size: 24rpx;
					color: #5c5c5c;
				}
			}
		}

		&_add {
			padding: 20rpx;
			background-color: #fff;
			display: flex;
			align-items: center;

			&_img {
				width: 50rpx;
				height: 50rpx;
			}

			&_text {
				margin-left: 10rpx;
				font-size: 24rpx;
				color: #999;
			}
		}
	}
</style>